/*******************************************************************************
 * Copyright (c) 2014-9-18 @author <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a>.
 * All rights reserved.
 *
 * Contributors:
 *     <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a> - initial API and implementation
 ******************************************************************************/
package com.foreveross.project.projectonline.jfinal.plugin;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.time.DateUtils;
import org.iff.infra.util.Logger;

import freemarker.template.DefaultObjectWrapper;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;

/**
 * @author <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a> 
 * @since 2014-9-18
 */
public class FreeMarkerExt {
	private static Map<String, Object> extMap = new HashMap<String, Object>();

	public static Map<String, Object> getExtMap() {
		if (extMap.isEmpty()) {
			extMap.put("ds2num", DateStr2Number.instance);
			extMap.put("toDate", ToDate.instance);
		}
		return extMap;
	}

	public static class ToDate implements TemplateMethodModelEx {
		public static final ToDate instance = new ToDate();

		public Object exec(List args) throws TemplateModelException {
			if (args.size() < 1) {
				return DefaultObjectWrapper.getDefaultInstance().wrap(null);
			}
			Object o = args.get(0);
			if (o == null) {
				return DefaultObjectWrapper.getDefaultInstance().wrap(null);
			}
			if (o instanceof TemplateModel) {
				o = DefaultObjectWrapper.getDefaultInstance().unwrap((TemplateModel) o);
			}
			if (o instanceof Date) {
				return (Date) o;
			}
			if (o instanceof Number) {
				return new Date(((Number) o).longValue());
			}
			if (o instanceof String) {
				try {
					String dateStr = (String) o;
					return DefaultObjectWrapper.getDefaultInstance().wrap(
							DateUtils.parseDate(dateStr, "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyyMMdd",
									"yyyyMMddHHmmss"));
				} catch (Exception e) {
					Logger.warn("[FreeMarker][ToDate]", e);
				}
				return DefaultObjectWrapper.getDefaultInstance().wrap(null);
			}
			return DefaultObjectWrapper.getDefaultInstance().wrap(null);
		}
	}

	public static class DateStr2Number implements TemplateMethodModelEx {
		public static final DateStr2Number instance = new DateStr2Number();

		public Object exec(List args) throws TemplateModelException {
			if (args.size() < 1) {
				return -1;
			}
			Object o = args.get(0);
			if (o == null) {
				return -1;
			}
			if (o instanceof TemplateModel) {
				o = DefaultObjectWrapper.getDefaultInstance().unwrap((TemplateModel) o);
			}
			if (o instanceof Date) {
				return ((Date) o).getTime();
			}
			if (o instanceof String) {
				try {
					String dateStr = (String) o;
					return DateUtils.parseDate(dateStr, "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyyMMdd",
							"yyyyMMddHHmmss").getTime();
				} catch (Exception e) {
					Logger.warn("[FreeMarker][DateStr2Number]", e);
				}
				return -1;
			}
			return -1;
		}
	}
}
